package com.example.kafka;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.clients.consumer.ConsumerRecords;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class KafkaConsumerApp {
    /**
     * sasl认证  消费者
     * @param args
     */
    public static void main(String[] args) {
        // 创建消费者配置对象
        Properties properties = new Properties();

        // 添加Kafka服务器地址和认证信息
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:19093,192.168.200.130:19092,192.168.200.130:19094");

        // Key和Value的反序列化器
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

        // SASL认证配置
        properties.put("security.protocol", "SASL_PLAINTEXT");
        properties.put("sasl.mechanism", "PLAIN");
        properties.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required " +
                "username=\"admin\" password=\"123456\";");

        // 消费者组ID
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "con-1"); // 消费者组
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); // 从最早的消息开始消费  

        // 创建Kafka消费者对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        // 订阅"first"主题
        ArrayList<String> topics = new ArrayList<>();
        topics.add("first");
        consumer.subscribe(topics);

        try {
            // 无限循环，不断拉取数据
            while (true) {
                // 拉取消息
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(10));

                // 处理拉取到的每个消息
                for (TopicPartition partition : records.partitions()) {
                    List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);

                    String topic = partition.topic();
                    int partitionId = partition.partition();
                    int size = partitionRecords.size();
                    System.out.printf("--- 获取topic: %s, 分区位置：%d, 消息总数：%d%n", topic, partitionId, size);

                    // 处理每条消息
                    for (ConsumerRecord<String, String> record : partitionRecords) {
                        String key = record.key();
                        String value = record.value();
                        long offset = record.offset();
                        System.out.printf("消费消息 key：%s, value：%s, 消息offset: %s%n", key, value, offset);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 确保消费者关闭时释放资源
            consumer.close();
            System.out.println("消费者已关闭");
        }
    }
}
